package resource.i18n;

import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;

import cn.dreampie.orm.transaction.Transaction;
import cn.dreampie.route.annotation.API;
import cn.dreampie.route.annotation.GET;
import cn.dreampie.route.annotation.POST;
import resource.ApiResource;
import resource.RestMsg;
import resource.i18n.model.I18n;

/**
 * Created by hadong on 15-1-22.
 */
@API("/i18n")
public class I18nResource extends ApiResource {

	@GET
	public List<I18n> findAllI18n() {
		return I18n.dao.findAllI18n();
	}

	@GET("/id/:id")
	public I18n findI18nById(Integer id) {
		return I18n.dao.findI18nById(id);
	}

	@GET("/type/:tid")
	public List<I18n> findI18nByTypeId(Integer tid) {
		return I18n.dao.findI18nByTypeId(tid);
	}

	@GET("/version/:vid")
	public List<I18n> findI18nByVersionId(Integer vid) {
		return I18n.dao.findI18nByVersionId(vid);
	}

	@POST("/add")
	public RestMsg addI18n() {
		I18n i18n = getModelParams(I18n.class);
		I18n i18n_check = findI18nByLabelNameAndTypeId(i18n.get("label_name", String.class), i18n.get("type_id", Integer.class));
		if (null != i18n_check) {
			return new RestMsg(500, "add failed,because the lanel_name and type_id is existed!");
		}
		i18n.set("create_dt", new Date()).set("create_id", getCurrentUserId()).save();
		return new RestMsg();
	}

	@POST("/update/:id")
	public RestMsg updateI18n(Integer id) {
		I18n old_i18n = I18n.dao.findFirstBy("id=?", id);
		if (null == old_i18n) {
			return new RestMsg(500, "this id is not correct!");
		}
		I18n i18n = getModelParams(I18n.class);
		i18n.remove("create_dt", "create_id");
		I18n i18n_check = findI18nByLabelNameAndTypeId(i18n.get("label_name", String.class), i18n.get("type_id", Integer.class));
		if (null != i18n_check) {
			return new RestMsg(500, "update failed,because the lanel_name and type_id is existed!");
		}
		old_i18n.setAttrs(i18n).set("update_dt", new Date()).set("update_id", getCurrentUserId()).update();
		return new RestMsg();
	}

	@GET("/delete/:id")
	public RestMsg deleteI18n(int id) {
		if (!I18n.dao.deleteBy("id=?", id)) {
			return new RestMsg(500, "delete failed");
		}
		return new RestMsg();
	}

	@POST("/batch")
	@Transaction
	public RestMsg BatchI18n() {
		Integer versionId = getParam("version_id");
		Integer typeId = getParam("type_id");
		List<JSONObject> i18ns = getParam("i18ns");
		for (JSONObject i18n : i18ns) {
			I18n i18n_old = findI18nByLabelNameAndTypeId(i18n.getString("label_name"), typeId);
			I18n i18n_new = new I18n().set("version_id", versionId).set("type_id", typeId).set("create_dt", new Date()).set("create_id", getCurrentUserId());
			for (Entry<String, Object> entry : i18n.entrySet()) {
				i18n_new.set(entry.getKey(), entry.getValue());
			}
			if (null != i18n_old) {
				i18n_old.setAttrs(i18n_new.remove("create_dt", "create_id")).set("update_dt", new Date()).set("update_id", getCurrentUserId()).update();
			} else {
				I18n.dao.save(i18n_new);
			}
		}
		return new RestMsg();
	}

	@POST("/search")
	public List<I18n> searchI18n(String search) {
		return I18n.dao.searchI18n(search);
	}

	public I18n findI18nByLabelNameAndTypeId(String labelName, Integer typeId) {
		return I18n.dao.findI18nByLabelNameAndTypeId(labelName, typeId);
	}
}
